package com.monicash.monicash.widge;


import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.monicash.monicash.R;
import com.monicash.monicash.util.DensityUtil;
import com.monicash.monicash.util.StringUtils;


public class HeaderViewLayout extends FrameLayout {
    private final Context mContext;
    private final int headerBackColor;//整个View的背景色

    //左边
    private final int flLeftLayoutBackGroundColor;
    private final int ivLeftBackImageViewResource;
    private final int ivLeftBackImageViewBackGroundColor;
    private final int ivLeftBackImageWidth;
    private final int ivLeftBackImageHeight;

    //中间文字
    private final int middleTextSize;
    private final String middleText;
    private final int middleTextColor;

    //右边文字
    private final int rightTextSize;
    private final String rightText;
    private final int rightTextColor;

    //右边图片
    private final int flRightLayoutBackGroundColor;
    private final int ivRightBackImageViewBackGroundColor;
    private final int ivRightBackImageViewResource;
    private final int ivRightBackImageWidth;
    private final int ivRightBackImageHeight;

    public HeaderViewLayout(@NonNull Context context) {
        this(context, null);
    }

    public HeaderViewLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HeaderViewLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.HeaderViewLayout);
        headerBackColor = typedArray.getColor(R.styleable.HeaderViewLayout_headerBackColor, ContextCompat.getColor(mContext, R.color.white));

        //左边
        flLeftLayoutBackGroundColor = typedArray.getColor(R.styleable.HeaderViewLayout_headerLeftFrameBackColor, 0);
        ivLeftBackImageViewResource = typedArray.getResourceId(R.styleable.HeaderViewLayout_headerLeftImage, R.drawable.icon_back_black);
        ivLeftBackImageViewBackGroundColor = typedArray.getColor(R.styleable.HeaderViewLayout_headerLeftImageBackColor, 0);
        ivLeftBackImageHeight = typedArray.getDimensionPixelOffset(R.styleable.HeaderViewLayout_headerLeftImageHeight, DensityUtil.getInstance().dip2px(25));
        ivLeftBackImageWidth = typedArray.getDimensionPixelOffset(R.styleable.HeaderViewLayout_headerLeftImageWidth, DensityUtil.getInstance().dip2px(25));

        //中间
        middleText = typedArray.getString(R.styleable.HeaderViewLayout_headerMiddleText);
        middleTextSize = typedArray.getInt(R.styleable.HeaderViewLayout_headerMiddleTextSize, 18);
        middleTextColor = typedArray.getColor(R.styleable.HeaderViewLayout_headerMiddleTextColor, ContextCompat.getColor(mContext, R.color.white));

        //右边
        flRightLayoutBackGroundColor = typedArray.getColor(R.styleable.HeaderViewLayout_headerRightFrameBackColor, 0);

        //右边文字
        rightTextSize = typedArray.getInt(R.styleable.HeaderViewLayout_headerRightTextSize,14);
        rightTextColor = typedArray.getColor(R.styleable.HeaderViewLayout_headerRightTextColor, ContextCompat.getColor(mContext, R.color.black_333333));
        rightText = typedArray.getString(R.styleable.HeaderViewLayout_headerRightText);

        //右边图片
        ivRightBackImageViewBackGroundColor = typedArray.getColor(R.styleable.HeaderViewLayout_headerRightImageBackColor, 0);
        ivRightBackImageViewResource = typedArray.getResourceId(R.styleable.HeaderViewLayout_headerRightImage, 0);
        ivRightBackImageWidth = typedArray.getDimensionPixelOffset(R.styleable.HeaderViewLayout_headerRightImageWidth,DensityUtil.getInstance().dip2px(25));
        ivRightBackImageHeight = typedArray.getDimensionPixelOffset(R.styleable.HeaderViewLayout_headerRightImageHeight,DensityUtil.getInstance().dip2px(25));

        typedArray.recycle();
        init();
    }

    private void init() {
        //左边
        FrameLayout frameBackLayout = new FrameLayout(mContext);
        frameBackLayout.setId(R.id.base_header_framelayout_back);
        FrameLayout.LayoutParams flrameBackLayoutLayoutParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        flrameBackLayoutLayoutParams.gravity = Gravity.CENTER_VERTICAL;
        frameBackLayout.setPadding(DensityUtil.getInstance().dip2px(10), 0, DensityUtil.getInstance().dip2px(10), 0);
        flrameBackLayoutLayoutParams.width = ivLeftBackImageWidth + DensityUtil.getInstance().dip2px(20) + DensityUtil.getInstance().dip2px(5);
        frameBackLayout.setLayoutParams(flrameBackLayoutLayoutParams);
        if (flLeftLayoutBackGroundColor != 0) {
            frameBackLayout.setBackgroundColor(flLeftLayoutBackGroundColor);
        }


        ImageView ivBackImageView = new ImageView(mContext);
        FrameLayout.LayoutParams ivBackImageLayoutParams = new FrameLayout.LayoutParams(ivLeftBackImageWidth, ivLeftBackImageHeight);
        ivBackImageView.setScaleType(ImageView.ScaleType.FIT_XY);
        ivBackImageView.setLayoutParams(ivBackImageLayoutParams);
        ivBackImageLayoutParams.gravity = Gravity.CENTER;
        if (ivLeftBackImageViewBackGroundColor != 0) {
            ivBackImageView.setBackgroundColor(ivLeftBackImageViewBackGroundColor);
        }

        ivBackImageView.setImageResource(ivLeftBackImageViewResource);
        frameBackLayout.addView(ivBackImageView);
        this.addView(frameBackLayout);

        //中间文字
        TextView middleTextView = new TextView(mContext);
        middleTextView.setId(R.id.tv_base_header_title);
        FrameLayout.LayoutParams middleTextViewLayoutParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        middleTextViewLayoutParams.gravity = Gravity.CENTER;
        middleTextView.setTextSize(middleTextSize);
        middleTextView.setGravity(Gravity.CENTER);
        middleTextView.setTextColor(middleTextColor);
        middleTextView.setText(StringUtils.isEmptyValue(middleText));
        middleTextView.setLayoutParams(middleTextViewLayoutParams);
        this.addView(middleTextView);

        //右边
        FrameLayout frameRightLayout = new FrameLayout(mContext);
        frameRightLayout.setId(R.id.base_header_framelayout_right);
        FrameLayout.LayoutParams frameRightLayoutLayoutParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        frameRightLayoutLayoutParams.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
        frameRightLayout.setLayoutParams(frameRightLayoutLayoutParams);
        frameRightLayout.setPadding(DensityUtil.getInstance().dip2px(10), 0, DensityUtil.getInstance().dip2px(10), 0);
        if (flRightLayoutBackGroundColor != 0) {
            frameRightLayout.setBackgroundColor(flRightLayoutBackGroundColor);
        }
        if (!StringUtils.isEmpty(rightText) || ivRightBackImageViewResource != 0) {
            if (!StringUtils.isEmpty(rightText)) {//显示右边文字
                TextView rightTextView = new TextView(mContext);
                rightTextView.setId(R.id.tv_baseheader_right_text);
                FrameLayout.LayoutParams rightTextViewLayoutParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
                rightTextViewLayoutParams.gravity = Gravity.CENTER;
                rightTextView.setTextSize(rightTextSize);
                rightTextView.setGravity(Gravity.CENTER);
                rightTextView.setText(StringUtils.isEmptyValue(rightText));
                rightTextView.setTextColor(rightTextColor);
                rightTextView.setLayoutParams(rightTextViewLayoutParams);
                frameRightLayout.addView(rightTextView);
            } else {
                ImageView ivRightImageView = new ImageView(mContext);
                ivRightImageView.setId(R.id.iv_baseheader_right_iv);
                FrameLayout.LayoutParams ivRightImageViewLayoutParams = new FrameLayout.LayoutParams(ivRightBackImageWidth, ivRightBackImageHeight);
                ivRightImageViewLayoutParams.gravity = Gravity.CENTER;
                ivRightImageView.setScaleType(ImageView.ScaleType.FIT_XY);
                ivRightImageView.setLayoutParams(ivRightImageViewLayoutParams);
                if (ivRightBackImageViewBackGroundColor != 0) {
                    ivRightImageView.setBackgroundColor(ivRightBackImageViewBackGroundColor);
                }
                ivRightImageView.setImageResource(ivRightBackImageViewResource);
                frameRightLayout.addView(ivRightImageView);
            }
        }
        this.addView(frameRightLayout);
        this.setId(R.id.base_header_framelayout);
        this.setBackgroundColor(headerBackColor);
    }


    public void setRightView(View view){
        FrameLayout.LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
        layoutParams.gravity = Gravity.CENTER;
        view.setLayoutParams(layoutParams);
        FrameLayout frameRightLayout = this.findViewById(R.id.base_header_framelayout_right);
        frameRightLayout.removeAllViews();
        frameRightLayout.addView(view);
    }
}

